package com.guanghua.brick.html.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.db.PageInfo;
import com.guanghua.brick.html.IComponent;
import com.guanghua.brick.html.IDataSource;
import com.guanghua.brick.html.WebControl;

public class ContainPagerTag extends BodyTagSupport implements IComponent{
	
	private static Log logger = LogFactory.getLog(ContainPagerTag.class);
	
	private String id = null;
	private String name = null;
	private String html = "";
	
	private String dataSource = null;
	private PageInfo data = null;
	private String action = null;
	private String prefix = null;
	private String start = "start";
	private String width = "100%";
	
	

	public int doEndTag() throws JspException {
		initData();
		//set start
		if (this.data != null) {
			int s = (this.data.getPageNo() - 1) * this.data.getPageSize()+1;
			pageContext.setAttribute(start, String.valueOf(s));
		}
		
		try {
			Tag tag = this.getParent();
			//如果父tag是table，那么set 父tag的属性
			if (tag != null && tag instanceof ContainTableTag) {
				
				ContainTableTag t = (ContainTableTag) tag;
				if (this.action != null && t.getAction() == null) t.setAction(this.action);
				if (this.prefix != null && t.getParent() == null) t.setPrefix(this.prefix);
				
				if (this.data != null) {
					StringBuffer s = new StringBuffer();
					s.append("pageSize=\"")
						.append(this.data.getPageSize())
						.append("\" pageNo=\"")
						.append(this.data.getPageNo())
						.append("\" pageCount=\"")
						.append(this.data.getPageCount())
						.append("\" rowCount=\"")
						.append(this.data.getRowCount())
						.append("\" pagerWidth=\"")
						.append(this.getWidth())
						.append("\"");
						
					
					if (t.getHtml() == null || t.getHtml().equals("")) {
						t.setHtml(s.toString());
					} else {
						s.append(" " + t.getHtml());
						t.setHtml(s.toString());
					}
				}
				
			//输入的界面上
			} else {
				JspWriter out = pageContext.getOut();
				StringBuffer html = new StringBuffer();
				html.append("<script language=\"javascript\">\n");
				html.append("	var ")
					.append(this.getId())
					.append(" = new Pager(\"")
					.append(this.getId())
					.append("\", \"")
					.append(this.getWidth())
					.append("\"");
				
				if (this.prefix != null) {
					html.append(", \"")
					.append(this.prefix)
					.append("\"");
				}
				
				html.append(");\n");
				
				if (this.data != null) {
					html.append("	").append(this.getId()).append(".pageNo = ").append(this.getData().getPageNo()).append(";\n");
					html.append("	").append(this.getId()).append(".pageSize = ").append(this.getData().getPageSize()).append(";\n");
					html.append("	").append(this.getId()).append(".pageCount = ").append(this.getData().getPageCount()).append(";\n");
					html.append("	").append(this.getId()).append(".rowCount = ").append(this.getData().getRowCount()).append(";\n");
				}
				
				if (this.action != null) {
					html.append("	").append(this.getId()).append(".action = \"").append(WebAppContextTag.getWebAppContext(pageContext) + this.action).append("\";\n");
				}
				
				html.append("	").append(this.getId()).append(".write();\n");
				html.append("</script>\n");
				
				out.print(html.toString());
			}
		} catch (IOException e) {
			logger.error("io exception on print pager html", e);
		}
		return BodyTagSupport.EVAL_PAGE;
	}
	
	private void initData() {
		if (this.data == null) {
			if (this.dataSource != null) {
				//调用el解析器解析
				IDataSource ds = WebControl.getDataSourceParser(pageContext);
				if (ds != null) 
					this.data = (PageInfo)ds.parseDataSource(pageContext, dataSource, PageInfo.class);
			}
		}
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public PageInfo getData() {
		return data;
	}

	public void setData(PageInfo data) {
		this.data = data;
	}

	public String getDataSource() {
		return dataSource;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}
	
	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

}
